part of '../../repository.dart';

const _kWebPaymentMethodPaymentSystemValue = 'yookassa';

class _YookassaPaymentMethodDto extends Equatable implements _WebPaymentMethodDto {
  const _YookassaPaymentMethodDto._internal({
    required this.id,
    required this.userId,
    required this.saved,
    required this.bankCardInfoDto,
    required this.failureDto,
  });

  @override
  final String id;

  @override
  final String userId;

  @override
  final bool saved;

  @override
  final _YookassaBankCardInfoDto bankCardInfoDto;

  @override
  final _WebPaymentMethodFailureDto? failureDto;

  @override
  String get paymentSystem => _kWebPaymentMethodPaymentSystemValue;

  @override
  List<Object?> get props {
    return [
      id,
      userId,
      saved,
      bankCardInfoDto,
      failureDto,
    ];
  }

  factory _YookassaPaymentMethodDto.fromJson(Map<String, dynamic> json) {
    final yookassaBankCardInfoJson =
        json[_kWebPaymentMethodBankCardInfoKey] as Map<String, dynamic>;
    final bankCardInfoDto = _YookassaBankCardInfoDto.fromJson(yookassaBankCardInfoJson);

    final failureString = json[_kWebPaymentMethodFailureKey] as String?;

    _WebPaymentMethodFailureDto? failureDto;

    if (failureString != null) {
      failureDto = _WebPaymentMethodFailureDto.fromString(failureString);
    }

    return _YookassaPaymentMethodDto._internal(
      id: json[_kWebPaymentMethodIdKey],
      userId: json[_kWebPaymentMethodUserIdKey],
      saved: json[_kWebPaymentMethodSavedKey],
      bankCardInfoDto: bankCardInfoDto,
      failureDto: failureDto,
    );
  }

  @override
  YookassaPaymentMethod toEntity() {
    return YookassaPaymentMethod(
      id: id,
      userId: userId,
      saved: saved,
      bankCardInfo: bankCardInfoDto.toEntity(),
      failure: failureDto?.toEntity(),
    );
  }
}
